
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>窗口函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.Hive实战.html" />
    
    
    <link rel="prev" href="1.数据处理.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    Hadoop
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/1Hadoop.md">
            
                <span>
            
                    
                    Hadoop
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    HIVE
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="1.数据处理.html">
            
                <a href="1.数据处理.html">
            
                    
                    数据处理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.2" data-path="2.窗口函数.html">
            
                <a href="2.窗口函数.html">
            
                    
                    窗口函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="3.Hive实战.html">
            
                <a href="3.Hive实战.html">
            
                    
                    Hive实战
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    Flume
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/1.Flume入门.md">
            
                <span>
            
                    
                    Flume入门与实践
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter4/数据可视化.md">
            
                <span>
            
                    
                    数据可视化
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >窗口函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x76F8;&#x5173;&#x51FD;&#x6570;&#x8BF4;&#x660E;">&#x76F8;&#x5173;&#x51FD;&#x6570;&#x8BF4;&#x660E;</h2>
<ul>
<li>OVER()&#xFF1A;&#x6307;&#x5B9A;&#x5206;&#x6790;&#x51FD;&#x6570;&#x5DE5;&#x4F5C;&#x7684;&#x6570;&#x636E;&#x7A97;&#x53E3;&#x5927;&#x5C0F;&#xFF0C;&#x8FD9;&#x4E2A;&#x6570;&#x636E;&#x7A97;&#x53E3;&#x5927;&#x5C0F;&#x53EF;&#x80FD;&#x4F1A;&#x968F;&#x7740;&#x884C;&#x7684;&#x53D8;&#x800C;&#x53D8;&#x5316;</li>
<li>CURRENT ROW&#xFF1A;&#x5F53;&#x524D;&#x884C;</li>
<li>n PRECEDING&#xFF1A;&#x5F80;&#x524D;n&#x884C;&#x6570;&#x636E;</li>
<li>n FOLLOWING&#xFF1A;&#x5F80;&#x540E;n&#x884C;&#x6570;&#x636E;</li>
<li>UNBOUNDED&#xFF1A;&#x8D77;&#x70B9;&#xFF0C;</li>
<li>UNBOUNDED PRECEDING &#x8868;&#x793A;&#x4ECE;&#x524D;&#x9762;&#x7684;&#x8D77;&#x70B9;&#xFF0C;</li>
<li>UNBOUNDED FOLLOWING&#x8868;&#x793A;&#x5230;&#x540E;&#x9762;&#x7684;&#x7EC8;&#x70B9;</li>
<li>LAG(col,n)&#xFF1A;&#x5F80;&#x524D;&#x7B2C;n&#x884C;&#x6570;&#x636E;</li>
<li>LEAD(col,n)&#xFF1A;&#x5F80;&#x540E;&#x7B2C;n&#x884C;&#x6570;&#x636E;</li>
<li>NTILE(n)&#xFF1A;&#x628A;&#x6709;&#x5E8F;&#x5206;&#x533A;&#x4E2D;&#x7684;&#x884C;&#x5206;&#x53D1;&#x5230;&#x6307;&#x5B9A;&#x6570;&#x636E;&#x7684;&#x7EC4;&#x4E2D;&#xFF0C;&#x5404;&#x4E2A;&#x7EC4;&#x6709;&#x7F16;&#x53F7;&#xFF0C;&#x7F16;&#x53F7;&#x4ECE;1&#x5F00;&#x59CB;&#xFF0C;&#x5BF9;&#x4E8E;&#x6BCF;&#x4E00;&#x884C;&#xFF0C;NTILE&#x8FD4;&#x56DE;&#x6B64;&#x884C;&#x6240;&#x5C5E;&#x7684;&#x7EC4;&#x7684;&#x7F16;&#x53F7;&#x3002;&#x6CE8;&#x610F;&#xFF1A;n&#x5FC5;&#x987B;&#x4E3A;int&#x7C7B;&#x578B;&#x3002;</li>
</ul>
<h2 id="&#x6570;&#x636E;&#x96C6;">&#x6570;&#x636E;&#x96C6;</h2>
<pre><code>name&#xFF0C;orderdate&#xFF0C;cost
tony,2017-01-02,15
jack,2017-02-03,23
tony,2017-01-04,29
jack,2017-01-05,46
jack,2017-04-06,42
tony,2017-01-07,50
jack,2017-01-08,55
mart,2017-04-08,62
mart,2017-04-09,68
neil,2017-05-10,12
mart,2017-04-11,75
neil,2017-06-12,80
mart,2017-04-13,94
</code></pre><pre><code class="lang-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">EXISTS</span> business(
    <span class="hljs-keyword">name</span> <span class="hljs-keyword">string</span>,
    orderdate <span class="hljs-keyword">string</span>,
    <span class="hljs-keyword">cost</span> <span class="hljs-built_in">int</span>
) <span class="hljs-keyword">ROW</span> <span class="hljs-keyword">FORMAT</span> <span class="hljs-keyword">DELIMITED</span> <span class="hljs-keyword">FIELDS</span> <span class="hljs-keyword">TERMINATED</span> <span class="hljs-keyword">BY</span> <span class="hljs-string">&apos;,&apos;</span>;
<span class="hljs-keyword">load</span> <span class="hljs-keyword">data</span> <span class="hljs-keyword">local</span> inpath <span class="hljs-string">&apos;path/to...&apos;</span> <span class="hljs-keyword">into</span> <span class="hljs-keyword">table</span> business;
</code></pre>
<h2 id="&#x9700;&#x6C42;">&#x9700;&#x6C42;</h2>
<ol>
<li>&#x67E5;&#x8BE2;&#x5728;2017&#x5E74;4&#x6708;&#x4EFD;&#x8D2D;&#x4E70;&#x8FC7;&#x7684;&#x987E;&#x5BA2;&#x53CA;&#x603B;&#x4EBA;&#x6570;</li>
<li>&#x67E5;&#x8BE2;&#x987E;&#x5BA2;&#x7684;&#x8D2D;&#x4E70;&#x660E;&#x7EC6;&#x53CA;&#x6708;&#x8D2D;&#x4E70;&#x603B;&#x989D;</li>
<li>&#x4E0A;&#x8FF0;&#x7684;&#x573A;&#x666F;,&#x8981;&#x5C06;cost&#x6309;&#x7167;&#x65E5;&#x671F;&#x8FDB;&#x884C;&#x7D2F;&#x52A0;</li>
<li>&#x67E5;&#x8BE2;&#x987E;&#x5BA2;&#x4E0A;&#x6B21;&#x7684;&#x8D2D;&#x4E70;&#x65F6;&#x95F4;</li>
<li>&#x67E5;&#x8BE2;&#x524D;20%&#x65F6;&#x95F4;&#x7684;&#x8BA2;&#x5355;&#x4FE1;&#x606F;</li>
</ol>
<h2 id="&#x53C2;&#x8003;&#x7B54;&#x6848;">&#x53C2;&#x8003;&#x7B54;&#x6848;</h2>
<p>&#xFF08;1&#xFF09; &#x67E5;&#x8BE2;&#x5728;2017&#x5E74;4&#x6708;&#x4EFD;&#x8D2D;&#x4E70;&#x8FC7;&#x7684;&#x987E;&#x5BA2;&#x53CA;&#x603B;&#x4EBA;&#x6570;</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">name</span>,<span class="hljs-keyword">COUNT</span>(*) <span class="hljs-keyword">OVER</span>() <span class="hljs-keyword">FROM</span> business <span class="hljs-keyword">WHERE</span> <span class="hljs-keyword">substring</span>(orderdate,<span class="hljs-number">1</span>,<span class="hljs-number">7</span>) = <span class="hljs-string">&apos;2017-04&apos;</span> <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> <span class="hljs-keyword">name</span>;
</code></pre>
<p>&#xFF08;2&#xFF09; &#x67E5;&#x8BE2;&#x987E;&#x5BA2;&#x7684;&#x8D2D;&#x4E70;&#x660E;&#x7EC6;&#x53CA;&#x6708;&#x8D2D;&#x4E70;&#x603B;&#x91CF;</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">name</span>,orderdate,<span class="hljs-keyword">cost</span>,<span class="hljs-keyword">SUM</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">month</span>(orderdate)) sum_of_month <span class="hljs-keyword">FROM</span> business;
</code></pre>
<p>&#xFF08;3&#xFF09; &#x4E0A;&#x8FF0;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x8981;&#x5C06;cost&#x6309;&#x7167;&#x65E5;&#x671F;&#x8FDB;&#x884C;&#x7D2F;&#x52A0;</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">name</span>,orderdate,<span class="hljs-keyword">cost</span>,<span class="hljs-keyword">SUM</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate) sum_of_cost <span class="hljs-keyword">FROM</span> business;
</code></pre>
<p>&#x66F4;&#x591A;&#x573A;&#x666F;</p>
<pre><code class="lang-sql"><span class="hljs-keyword">select</span> <span class="hljs-keyword">name</span>,orderdate,<span class="hljs-keyword">cost</span>, 
<span class="hljs-keyword">sum</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">over</span>() <span class="hljs-keyword">as</span> sample1,<span class="hljs-comment">--&#x6240;&#x6709;&#x884C;&#x76F8;&#x52A0; </span>
<span class="hljs-keyword">sum</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span>) <span class="hljs-keyword">as</span> sample2,<span class="hljs-comment">--&#x6309;name&#x5206;&#x7EC4;&#xFF0C;&#x7EC4;&#x5185;&#x6570;&#x636E;&#x76F8;&#x52A0; </span>
<span class="hljs-keyword">sum</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate) <span class="hljs-keyword">as</span> sample3,<span class="hljs-comment">--&#x6309;name&#x5206;&#x7EC4;&#xFF0C;&#x7EC4;&#x5185;&#x6570;&#x636E;&#x7D2F;&#x52A0; </span>
<span class="hljs-keyword">sum</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate <span class="hljs-keyword">rows</span> <span class="hljs-keyword">between</span> <span class="hljs-keyword">UNBOUNDED</span> <span class="hljs-keyword">PRECEDING</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">current</span> <span class="hljs-keyword">row</span> ) <span class="hljs-keyword">as</span> sample4 ,<span class="hljs-comment">--&#x548C;sample3&#x4E00;&#x6837;,&#x7531;&#x8D77;&#x70B9;&#x5230;&#x5F53;&#x524D;&#x884C;&#x7684;&#x805A;&#x5408; </span>
<span class="hljs-keyword">sum</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate <span class="hljs-keyword">rows</span> <span class="hljs-keyword">between</span> <span class="hljs-number">1</span> <span class="hljs-keyword">PRECEDING</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">current</span> <span class="hljs-keyword">row</span>) <span class="hljs-keyword">as</span> sample5, <span class="hljs-comment">--&#x5F53;&#x524D;&#x884C;&#x548C;&#x524D;&#x9762;&#x4E00;&#x884C;&#x505A;&#x805A;&#x5408; </span>
<span class="hljs-keyword">sum</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate <span class="hljs-keyword">rows</span> <span class="hljs-keyword">between</span> <span class="hljs-number">1</span> <span class="hljs-keyword">PRECEDING</span> <span class="hljs-keyword">AND</span> <span class="hljs-number">1</span> <span class="hljs-keyword">FOLLOWING</span> ) <span class="hljs-keyword">as</span> sample6,<span class="hljs-comment">--&#x5F53;&#x524D;&#x884C;&#x548C;&#x524D;&#x8FB9;&#x4E00;&#x884C;&#x53CA;&#x540E;&#x9762;&#x4E00;&#x884C; </span>
<span class="hljs-keyword">sum</span>(<span class="hljs-keyword">cost</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate <span class="hljs-keyword">rows</span> <span class="hljs-keyword">between</span> <span class="hljs-keyword">current</span> <span class="hljs-keyword">row</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">UNBOUNDED</span> <span class="hljs-keyword">FOLLOWING</span> ) <span class="hljs-keyword">as</span> sample7 <span class="hljs-comment">--&#x5F53;&#x524D;&#x884C;&#x53CA;&#x540E;&#x9762;&#x6240;&#x6709;&#x884C; </span>
<span class="hljs-keyword">from</span> business;
</code></pre>
<p>&#xFF08;4&#xFF09; &#x67E5;&#x8BE2;&#x987E;&#x5BA2;&#x4E0A;&#x6B21;&#x7684;&#x8D2D;&#x4E70;&#x65F6;&#x95F4;</p>
<pre><code class="lang-sql"><span class="hljs-keyword">select</span> 
<span class="hljs-keyword">name</span>,
orderdate,
<span class="hljs-keyword">cost</span>, 
lag(orderdate,<span class="hljs-number">1</span>,<span class="hljs-string">&apos;1900-01-01&apos;</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate ) <span class="hljs-keyword">as</span> time1,
lag(orderdate,<span class="hljs-number">2</span>) <span class="hljs-keyword">over</span> (<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate) <span class="hljs-keyword">as</span> time2 
<span class="hljs-keyword">from</span> business;
</code></pre>
<p>&#xFF08;5&#xFF09; &#x67E5;&#x8BE2;&#x524D;20%&#x65F6;&#x95F4;&#x7684;&#x8BA2;&#x5355;</p>
<pre><code class="lang-sql"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> (
    <span class="hljs-keyword">select</span> <span class="hljs-keyword">name</span>,orderdate,<span class="hljs-keyword">cost</span>, ntile(<span class="hljs-number">5</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate) sorted
    <span class="hljs-keyword">from</span> business
) t
<span class="hljs-keyword">where</span> sorted = <span class="hljs-number">1</span>;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1.数据处理.html" class="navigation navigation-prev " aria-label="Previous page: 数据处理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.Hive实战.html" class="navigation navigation-next " aria-label="Next page: Hive实战">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"窗口函数","level":"1.3.2","depth":2,"next":{"title":"Hive实战","level":"1.3.3","depth":2,"path":"chapter2/3.Hive实战.md","ref":"chapter2/3.Hive实战.md","articles":[]},"previous":{"title":"数据处理","level":"1.3.1","depth":2,"path":"chapter2/1.数据处理.md","ref":"chapter2/1.数据处理.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter2/2.窗口函数.md","mtime":"2021-04-09T14:00:23.918Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-09T14:00:30.441Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

